package com.whut.work.score.service.impl;

import com.whut.work.base.model.Page;
import com.whut.work.comment.model.Comment;
import com.whut.work.score.dao.impl.ScoreDaoImpl;
import com.whut.work.score.model.Score;
import com.whut.work.score.service.IScoreService;
import com.whut.work.user.dao.impl.UserDaoImpl;
import com.whut.work.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zouyao on 2017/3/15.
 */
@Component
public class ScoreServiceImpl implements IScoreService{

    @Autowired
    private ScoreDaoImpl scoreDao;
    @Autowired
    private UserDaoImpl userDao;

    public Map<String, Object> getScoreRecordByUserId(Integer studentId,Integer teacherId) throws Exception {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        System.out.println(studentId+","+teacherId);
        Score score = scoreDao.findOne("from Score s where s.student.id='"+studentId+"' and s.teacherId='"+teacherId+"' ");
        if(score != null){
            returnMap.put("value", score);
            returnMap.put("message", "获取成功");
            returnMap.put("success", true);
        }else {
            returnMap.put("message", "无记录");
            returnMap.put("success", false);
        }

        return returnMap;
    }

    public Map<String, Object> setScoreForTeacher(Integer score, Integer studentId, Integer teacherId) throws Exception {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        Score scoreTemp = scoreDao.findOne(" from Score s where s.student.id='"+studentId+"' and s.teacherId='"+teacherId+"' ");

        if(scoreTemp != null){//已经评分过
            //更新user信息
            User teacher = userDao.findOne(" from User u where u.id='"+teacherId+"' ");
            teacher.setScore(teacher.getScore()-scoreTemp.getScore()+score);
            userDao.update(teacher);

            scoreDao.deleteWithHql(" delete Score s where s.id='"+scoreTemp.getId()+"' ");
            scoreTemp = new Score();
            scoreTemp.setScore(score);//覆盖更新
            scoreTemp.setCreateTime(new Date());
            User student = userDao.findOne(" from User u where u.id='"+studentId+"' ");
            scoreTemp.setStudent(student);
            scoreTemp.setTeacherId(teacherId);
            scoreDao.save(scoreTemp);
        }else {//没有评分过
            scoreTemp = new Score();
            scoreTemp.setCreateTime(new Date());
            scoreTemp.setScore(score);
            User student = userDao.findOne(" from User u where u.id='"+studentId+"' ");
            scoreTemp.setStudent(student);
            scoreTemp.setTeacherId(teacherId);
            scoreDao.save(scoreTemp);
            //更新user信息
            User teacher = userDao.findOne(" from User u where u.id='"+teacherId+"' ");
            if(teacher.getScore() == null){
                teacher.setScore(score);
            }else {
                teacher.setScore(teacher.getScore()+score);
            }
            userDao.update(teacher);
        }



        returnMap.put("value", scoreTemp);
        returnMap.put("message", "评分成功");
        returnMap.put("success", true);

        return returnMap;
    }

    public Page<Score> getScoreListOfOneTeacher(Integer currentPage, Integer pageSize, Integer id) throws Exception {
        String queryHql = " from Score s where s.teacherId='"+id+"' and s.student.id!='"+id+"' ";
        String countHql = "select count(*) from Score s where s.teacherId='"+id+"' and s.student.id!='"+id+"' ";
        Page<Score> returnPage = scoreDao.findPage(currentPage, pageSize, queryHql, countHql);

        return returnPage;
    }

}
